package example.com.zcq.mymj.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;
import java.util.List;

import example.com.zcq.mymj.R;
import example.com.zcq.mymj.bean.Toutiao;
import example.com.zcq.mymj.bean.ZiDian;
import example.com.zcq.mymj.net.HttpInterface;
import example.com.zcq.mymj.net.RetrofitHttp;
import example.com.zcq.mymj.ui.TopBar;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class XinHuaZiDianActivity extends AppCompatActivity {

    private TopBar mTopbarTitle;
    private SearchView mSvCus;
    /**
     * 拼音：hao
     */
    private TextView mTvPy;
    /**
     * 五笔：左右
     */
    private TextView mTvWubi;
    /**
     * 部首：hao
     */
    private TextView mTvBushou;
    /**
     * 笔画：左右
     */
    private TextView mTvBihua;
    /**
     * hasfhahsdkfahaksfalfa
     * laksjdflajsl
     * asjdlkajf;afjaldkla
     * fasfdla
     * fhakshdfas
     * fhashfda
     */
    private TextView mTvXiangjie;
    private TextView mTvZi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xin_hua_zi_dian);
        initView();
    }

    private void initView() {
        mTopbarTitle = (TopBar) findViewById(R.id.topbar_title);
        mSvCus = (SearchView) findViewById(R.id.sv_cus);
        mTvPy = (TextView) findViewById(R.id.tv_py);
        mTvZi = (TextView) findViewById(R.id.tv_zi);
        mTvWubi = (TextView) findViewById(R.id.tv_wubi);
        mTvBushou = (TextView) findViewById(R.id.tv_bushou);
        mTvBihua = (TextView) findViewById(R.id.tv_bihua);
        mTvXiangjie = (TextView) findViewById(R.id.tv_xiangjie);

        mSvCus.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                if (mSvCus != null) {
                    // 得到输入管理对象
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm != null) {
                        // 这将让键盘在所有的情况下都被隐藏，但是一般我们在点击搜索按钮后，输入法都会乖乖的自动隐藏的。
                        imm.hideSoftInputFromWindow(mSvCus.getWindowToken(), 0); // 输入法如果是显示状态，那么就隐藏输入法

                    }
                    mSvCus.clearFocus(); // 不获取焦点
                    search(s);

                }


                return true;
            }

            // 当搜索内容改变时触发该方法
            @Override
            public boolean onQueryTextChange(String s) {

                return false;
            }
        });
    }

    /**
     * 搜索
     */
    private void search(String s) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("key", "7edd69e7a3a0293d76fd3d073fcd0f51");
        hashMap.put("word", s);
        HttpInterface h = RetrofitHttp.getSimpleRetrofit();

        h.zidian(hashMap).enqueue(new Callback<ZiDian>() {
            @Override
            public void onResponse(Call<ZiDian> call, Response<ZiDian> response) {
                ZiDian b = response.body();
                if (b != null) {
                    ZiDian.ResultBean resultBean = b.getResult();

                    if (resultBean != null) {
                        mTvZi.setText(resultBean.getZi());
                        mTvBihua.setText("笔画：" + resultBean.getBihua());
                        mTvWubi.setText("五笔：" + resultBean.getWubi());
                        mTvPy.setText("拼音：" + resultBean.getPinyin());
                        mTvBushou.setText("部首：" + resultBean.getBushou());
                        List<String> list = resultBean.getXiangjie();
                        StringBuilder stringBuilder = new StringBuilder();
                        stringBuilder.append("详解：\n");

                        for (int i = 0; i < list.size(); i++) {
                            stringBuilder.append(list.get(i) + "\n");
                        }
                        mTvXiangjie.setText(stringBuilder.toString());
                    }

                }
            }

            @Override
            public void onFailure(Call<ZiDian> call, Throwable t) {

            }
        });


    }
}
